// pages/orderCenter/orderCenter.js
import { orderCenterModel } from '../../model/orderCenter'
import { config } from '../../config'
var http = new orderCenterModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    picked: 0,
    listArr: [],
    current: 1,
    size: 10,
    haveMore: true,
    totalCount: 0
  },
  // 点击全部
  all() {
    this.setData({
      picked: this.picked = 0,
      listArr: [],
      current:1
    })
    http.orderList('', '', 0).then(res => {
      this.setData({
        listArr: res.data.list,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击待付款
  toBePay() {
    this.setData({
      picked: this.picked = 1,
      listArr: [],
      current:1
    })
    http.orderList('', '', 1).then(res => {
      this.setData({
        listArr: res.data.list,
        totalCount: res.data.totalCount
      })
    })
  },
  //点击待发货
  toBeDelivered() {
    this.setData({
      picked: this.picked = 2,
      listArr: [],
      current:1
    })
    http.orderList('', '', 2).then(res => {
      this.setData({
        listArr: res.data.list,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击待收货
  toBeReceived() {
    this.setData({
      picked: 3,
      listArr: [],
      current:1
    })
    http.orderList('', '', 3).then(res => {
      this.setData({
        listArr: res.data.list,
        totalCount: res.data.totalCount
      })
    })
  },
  // 点击售后
  afterSale() {
    this.setData({
      picked: this.picked = 4,
      listArr: [],
      current:1
    })
    http.afterSaleList().then(res => {
      this.setData({
        listArr: res.data.list
      })
    })
  },
  // 滑动到底部
  scrollToBottom() {
    if (this.data.listArr.length < this.data.totalCount) {
      this.setData({
        current: this.data.current = this.data.current + 1
      })
      http.orderList(this.data.current, this.data.size, this.data.picked).then(res => {
        this.setData({
          listArr: this.data.listArr = this.data.listArr.concat(res.data.list)
        })
        // console.log(this.data.current)
        // console.log( this.data.listArr.push.apply(this.data.listArr,res.data.list))
      })
    }
  },
  // 取消订单
  cancelOrder(e) {
    let cancelIndex = this.data.listArr[e.currentTarget.dataset.bindex].id
    let type = this.data.picked
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要取消吗？',
      success: function (sm) {
        if (sm.confirm) {
          // console.log(cancelIndex)
          that.setData({
            listArr:[]
          })
          http.cancelOrder(cancelIndex).then(res=>{
            console.log(type)
            http.orderList('','',type).then(res => {
              that.setData({
                listArr: res.data.list,
                totalCount: res.data.totalCount
              })
              wx.showToast({
                title: '取消订单成功',
                icon: 'none',
                duration: 2000
              })
            })
          })
        }
      }
    })
  },
  // 退款
  refund(e){
    let refundIndex = this.data.listArr[e.currentTarget.dataset.bindex].id;
    let commodityId = this.data.listArr[e.currentTarget.dataset.bindex].cartCommodities[0].id;
    let type = this.data.picked
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要退款吗？',
      success: function (sm) {
        if (sm.confirm) {
          // console.log(cancelIndex)
          that.setData({
            listArr:[]
          })
          http.refundOrder(refundIndex,0,commodityId).then(res=>{
            console.log(type)
            http.orderList('','',type).then(res => {
              that.setData({
                listArr: res.data.list,
                totalCount: res.data.totalCount
              })
              wx.showToast({
                title: '申请售后成功',
                icon: 'none',
                duration: 2000
              })
            })
          })
        }
      }
    })
  },
  // 确认收货
  confirm(e){
    let confirmId = this.data.listArr[e.currentTarget.dataset.bindex].id;
    let type = this.data.picked
    let that = this
    wx.showModal({
      title: '提示',
      content: '确定要收货吗？',
      success: function (sm) {
        if (sm.confirm) {
          // console.log(cancelIndex)
          that.setData({
            listArr:[]
          })
          http.confirm(confirmId).then(res=>{
            console.log(type)
            http.orderList('','',type).then(res => {
              that.setData({
                listArr: res.data.list,
                totalCount: res.data.totalCount
              })
              wx.showToast({
                title: '确认收货成功',
                icon: 'none',
                duration: 2000
              })
            })
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    http.orderList().then(res => {
      this.setData({
        listArr: res.data.list,
        totalCount: res.data.totalCount
      });
      // console.log(this.data.current)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})